package com.easy.util;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.easy.bean.User;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;

public class SYS {
    //文件上传保存路径
    public static final String FILE_UPLOAD_DIR="D:\\easyupload\\";
   //上传文件访问前缀路径
    public static final String FILE_UPLOAD_URL="easyfile";

    public static final String SHOPPING_LOGIN_USER="SHOPPING_LOGIN_USER";

    public static final String SYSTEM_LOGIN_ADMIN="SYSTEM_LOGIN_ADMIN";

    public static final int ADDREE_MAX_NUMBER=6;

    public static final int[] HEX_CHAR_ARR={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
    public static int getLoginUserID(HttpSession session){
        User user=(User)session.getAttribute(SHOPPING_LOGIN_USER);
        return user.getId();
    }

    public static boolean check(String reg,String... params){
        boolean result=true;
        for(String item:params){
            System.out.println(item);
            if(item==null){
                return false;
            }
            if(!item.matches(reg)){
                return false;
            }
        }
        return result;
    }

    public static String digest(String pass) {
        //随机次数随机盐值加密
        int countIndex=(int)(Math.random()*HEX_CHAR_ARR.length);
        int saltIndex=(int)(Math.random()*HEX_CHAR_ARR.length);
        int count=HEX_CHAR_ARR[countIndex];
        char salt=(char)HEX_CHAR_ARR[saltIndex];
        char countChar=(char)count;
        pass=pass+salt;

        for(int i=0;i<count;i++){
            pass=DigestUtils.md5DigestAsHex(pass.getBytes());
        }
        String passwordText=salt+pass+countChar;
        return passwordText;
    }
    //登录验证方法
    public static boolean checkPass(String pass,String hexPass){
        char salt=hexPass.charAt(0);//盐值
        char countChar=hexPass.charAt(hexPass.length()-1);//次数
        pass=pass+salt;
        for(int i=0;i<countChar;i++){
            pass=DigestUtils.md5DigestAsHex(pass.getBytes());
        }
        String text=salt+pass+countChar;
        return text.equals(hexPass);
    }


}
